import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.geom.Rectangle2D;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Combat;
import org.rsbot.script.methods.Environment;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.GroundItems;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Players;
import org.rsbot.script.methods.Settings;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.methods.Tiles;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSGroundItem;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSModel;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"Pervy Shuya"}, keywords={"Combat"}, name="YakAttack PRo", version=1.4D, description="Settings in GUI")
public class YakAttack extends Script
  implements PaintListener, MessageListener
{
  private final int KILLYAKS = 0;
  private final int KILLSCRIPT = 1;
  private final int SPECIAL = 2;
  final ScriptManifest properties;
  private final int yakID = 5529;
  private final int[] Junk;
  private final int[] superStrength;
  private final int[] superDefense;
  private final int[] superAttack;
  private final int[] normalStrength;
  private final int[] normalDefense;
  private final int[] normalAttack;
  private final int[] normalRange;
  private final int[] combatPots;
  private final int[] potionSet;
  private final String[] potionsToDrink;
  private int speed;
  private int hp2EatAt;
  YakAttack.YakAttackProGUI gui;
  Thread t;
  private int CURR_WEP_ID;
  private int SPEC_WEP_ID;
  private int yaksKilled;
  private int yaksPerHour;
  private int arrowID;
  RSItem foodInBag;
  private final int bronzeArrow = 882;
  private final int ironArrow = 884;
  private final int steelArrow = 886;
  private final int mithrilArrow = 888;
  private final int adamantArrow = 890;
  private final int runeArrow = 892;
  private final int bronzeBolt = 877;
  private final int boneBolt = 8882;
  private final int blueriteBolt = 9139;
  private final int ironBolt = 9140;
  private final int steelBolt = 9141;
  private final int blackBolt = 13083;
  private final int mithrilBolt = 9142;
  private final int adamantBolt = 9143;
  private final int runeBolt = 9144;
  private final int broadBolt = 13280;
  private final int bronzeKnife = 864;
  private final int ironKnife = 863;
  private final int steelKnife = 865;
  private final int blackKnife = 869;
  private final int mithrilKnife = 866;
  private final int adamantKnife = 867;
  private final int runeKnife = 868;
  private int specialCost;
  private int Food;
  private final int bronzeDart = 806;
  private final int ironDart = 807;
  private final int steelDart = 808;
  private final int blackDart = 3093;
  private final int mithrilDart = 809;
  private final int adamantDart = 810;
  private final int runeDart = 811;
  private int rndSpec;
  private int rndSpecCtr;
  private final Timer timeRan;
  private String Status;
  private String arrowName;
  private final RSTile yakTile;
  private boolean wants2Eat;
  private boolean doSpec;
  private boolean guiWait;
  private boolean guiExit;
  private boolean Wait;
  private final int[] foodID;
  private final int[] arrowPack;
  private final int[] exptolevel;
  private final int[] startinglvl;
  private final int[] currentlvl;
  private final int[] startingexp;
  private final int[] currentexp;
  private final int[] diffexp;
  private final String[] statnames;

  public YakAttack()
  {
    this.KILLYAKS = 0; this.KILLSCRIPT = 1; this.SPECIAL = 2;

    this.properties = ((ScriptManifest)super.getClass().getAnnotation(ScriptManifest.class));

    this.yakID = 5529;

    this.Junk = new int[] { 10814, 10816, 10818, 526 };
    this.superStrength = new int[] { 2440, 157, 159, 161 };
    this.superDefense = new int[] { 2442, 163, 165, 167 };
    this.superAttack = new int[] { 2436, 145, 147, 149 };
    this.normalStrength = new int[] { 113, 115, 117, 119 };
    this.normalDefense = new int[] { 2432, 133, 135, 137 };
    this.normalAttack = new int[] { 2428, 121, 123, 125 };
    this.normalRange = new int[] { 2444, 169, 171, 173 };
    this.combatPots = new int[] { 9739, 9741, 9743, 9745 };
    this.potionSet = new int[] { 2440, 157, 159, 161, 2442, 163, 165, 167, 2436, 145, 147, 149, 113, 115, 117, 119, 2432, 133, 135, 137, 2428, 121, 123, 125, 2444, 169, 171, 173, 9739, 9741, 9743, 9745 };

    this.potionsToDrink = new String[] { "Super Attack", "Super Strength", "Super Defense", "Combat potion", "Normal Range", "Normal Attack", "Normal Strength", "Normal Defense" };

    this.t = new Thread(new YakAttack.YakAttackAntiBan(this, null));

    this.arrowID = -1;

    this.bronzeArrow = 882; this.ironArrow = 884; this.steelArrow = 886; this.mithrilArrow = 888; this.adamantArrow = 890; this.runeArrow = 892; this.bronzeBolt = 877; this.boneBolt = 8882; this.blueriteBolt = 9139; this.ironBolt = 9140; this.steelBolt = 9141; this.blackBolt = 13083; this.mithrilBolt = 9142; this.adamantBolt = 9143; this.runeBolt = 9144; this.broadBolt = 13280; this.bronzeKnife = 864; this.ironKnife = 863; this.steelKnife = 865; this.blackKnife = 869; this.mithrilKnife = 866; this.adamantKnife = 867; this.runeKnife = 868;

    this.specialCost = 0;

    this.bronzeDart = 806; this.ironDart = 807; this.steelDart = 808; this.blackDart = 3093; this.mithrilDart = 809; this.adamantDart = 810; this.runeDart = 811;

    this.rndSpec = 1000;

    this.timeRan = new Timer(0L);

    this.Status = "Starting";
    this.yakTile = new RSTile(2324, 3792);
    this.guiWait = true;
    this.foodID = new int[] { 1895, 1893, 1891, 4293, 2142, 291, 2140, 3228, 9980, 7223, 6297, 6293, 6295, 6299, 7521, 9988, 7228, 2878, 7568, 2343, 1861, 13433, 315, 325, 319, 3144, 347, 355, 333, 339, 351, 329, 3381, 361, 10136, 5003, 379, 365, 373, 7946, 385, 397, 391, 3369, 3371, 3373, 2309, 2325, 2333, 2327, 2331, 2323, 2335, 7178, 7180, 7188, 7190, 7198, 7200, 7208, 7210, 7218, 7220, 2003, 2011, 2289, 2291, 2293, 2295, 2297, 2299, 2301, 2303, 1891, 1893, 1895, 1897, 1899, 1901, 7072, 7062, 7078, 7064, 7084, 7082, 7066, 7068, 1942, 6701, 6703, 7054, 6705, 7056, 7060, 2130, 1985, 1993, 1989, 1978, 5763, 5765, 1913, 5747, 1905, 5739, 1909, 5743, 1907, 1911, 5745, 2955, 5749, 5751, 5753, 5755, 5757, 5759, 5761, 2084, 2034, 2048, 2036, 2217, 2213, 2205, 2209, 2054, 2040, 2080, 2277, 2225, 2255, 2221, 2253, 2219, 2281, 2227, 2223, 2191, 2233, 2092, 2032, 2074, 2030, 2281, 2235, 2064, 2028, 2187, 2185, 2229, 6883, 1971, 4608, 1883, 1885 }; this.arrowPack = new int[] { 882, 884, 886, 888, 890, 892, 877, 8882, 9139, 9140, 9141, 13083, 9142, 9143, 9144, 13280, 864, 863, 865, 869, 866, 867, 868, 806, 807, 808, 3093, 809, 810, 811 };

    this.exptolevel = new int[] { 0, 83, 174, 276, 388, 512, 650, 801, 969, 1154, 1358, 1584, 1833, 2107, 2411, 2746, 3115, 3523, 3973, 4470, 5018, 5624, 6291, 7028, 7842, 8740, 9730, 10824, 12031, 13363, 14833, 16456, 18247, 20224, 22406, 24815, 27473, 30408, 33648, 37224, 41171, 45529, 50339, 55649, 61512, 67983, 75127, 83014, 91721, 101333, 111945, 123660, 136594, 150872, 166636, 184040, 203254, 224466, 247886, 273742, 302288, 333804, 368599, 407015, 449428, 496254, 547953, 605032, 668051, 737627, 814445, 899257, 992895, 1096278, 1210421, 1336443, 1475581, 1629200, 1798808, 1986068, 2192818, 2421087, 2673114, 2951373, 3258594, 3597792, 3972294, 4385776, 4842295, 5346332, 5902831, 6517253, 7195629, 7944614, 8771558, 9684577, 10692629, 11805606, 13034431, 200000001 };

    this.startinglvl = new int[8];
    this.currentlvl = new int[8];
    this.startingexp = new int[8];
    this.currentexp = new int[8];
    this.diffexp = new int[8];
    this.statnames = new String[] { "MAGIC", "ATTACK", "DEFENSE", "CONSTITUTION", "PRAYER", "RANGE", "SLAYER", "STRENGTH" };
  }

  private boolean pickupArrows(int[] paramArrayOfInt, String paramString) {
    int i = 0;
    try {
      RSGroundItem localRSGroundItem = this.groundItems.getNearest(paramArrayOfInt);
      Point localPoint = this.calc.tileToScreen(localRSGroundItem.getLocation());
      if ((localRSGroundItem != null) && (!getMyPlayer().isMoving())) {
        i = 1;
        if (this.calc.pointOnScreen(localPoint)) {
          this.mouse.move(localPoint, localPoint.x, localPoint.y);
          sleep(random(100, 200));
          if ((this.menu.getItems().length > 1) && 
            (listContainsString(this.menu.getItems(), paramString)))
            if (this.menu.getItems()[0].contains(paramString)) {
              this.mouse.click(true);
              sleep(random(750, 1000));
            } else {
              this.mouse.click(false);
              sleep(random(500, 750));
              this.menu.doAction(paramString);
              sleep(random(750, 1000));
            }
        }
      }
    }
    catch (Exception localException)
    {
    }
    return i;
  }
  private boolean listContainsString(String[] paramArrayOfString, String paramString) {
    int i;
    try {
      for (i = paramArrayOfString.length - 1; i-- >= 0; )
        if (paramArrayOfString[i].contains(paramString))
          return true;
    }
    catch (Exception localException) {
    }
    return false;
  }

  private boolean handleArrows() {
    RSGroundItem localRSGroundItem = this.groundItems.getNearest(this.arrowPack);
    if ((this.arrowID != -1) && (getMyPlayer().getInteracting() == null)) {
      this.Status = "Picking up Arrows";
      if (this.tiles.doAction(localRSGroundItem.getLocation(), 0.5D, 0.5D, 0, this.arrowName))
        return true;
      if (!this.tiles.doAction(localRSGroundItem.getLocation(), 0.5D, 0.5D, 0, this.arrowName))
      {
        pickupArrows(this.arrowPack, new StringBuilder().append("Take ").append(this.arrowName).toString());
        sleep(random(400, 600));
        return true;
      }
    }
    return false;
  }

  private void drinkPot(String paramString)
  {
    int k;
    if (paramString.equals("Super Attack")) {
      for (k : this.superAttack) {
        if (this.skills.getCurrentLevel(0) > this.skills.getRealLevel(0) + random(2, 4))
          continue;
        if (this.inventory.getCount(new int[] { k }) > 0) {
          this.inventory.getItem(this.superAttack).doAction("Drink");
          while (getMyPlayer().getAnimation() != -1) {
            sleep(random(300, 600));
          }
        }
      }
    }

    if (paramString.equals("Super Strength")) {
      for (k : this.superStrength) {
        if (this.skills.getCurrentLevel(2) > this.skills.getRealLevel(2) + random(2, 4))
          continue;
        if (this.inventory.getCount(new int[] { k }) > 0) {
          this.inventory.getItem(this.superStrength).doAction("Drink");
          while (getMyPlayer().getAnimation() != -1) {
            sleep(random(300, 600));
          }
        }
      }
    }

    if (paramString.equals("Super Defense")) {
      for (k : this.superDefense) {
        if (this.skills.getCurrentLevel(1) > this.skills.getRealLevel(1) + random(2, 4))
          continue;
        if (this.inventory.getCount(new int[] { k }) > 0) {
          this.inventory.getItem(this.superDefense).doAction("Drink");
          while (getMyPlayer().getAnimation() != -1) {
            sleep(random(300, 600));
          }
        }
      }
    }

    if (paramString.equals("Normal Range")) {
      for (k : this.normalRange) {
        if (this.skills.getCurrentLevel(4) > this.skills.getRealLevel(4) + random(2, 4))
          continue;
        if (this.inventory.getCount(new int[] { k }) > 0) {
          this.inventory.getItem(this.normalRange).doAction("Drink");
          while (getMyPlayer().getAnimation() != -1) {
            sleep(random(300, 600));
          }
        }
      }
    }

    if (paramString.equals("Normal Attack")) {
      for (k : this.normalAttack) {
        if (this.skills.getCurrentLevel(0) > this.skills.getRealLevel(0) + random(2, 4))
          continue;
        if (this.inventory.getCount(new int[] { k }) > 0) {
          this.inventory.getItem(this.normalAttack).doAction("Drink");
          while (getMyPlayer().getAnimation() != -1) {
            sleep(random(300, 600));
          }
        }
      }
    }

    if (paramString.equals("Normal Strength")) {
      for (k : this.normalStrength) {
        if (this.skills.getCurrentLevel(2) > this.skills.getRealLevel(2) + random(2, 4))
          continue;
        if (this.inventory.getCount(new int[] { k }) > 0) {
          this.inventory.getItem(this.normalStrength).doAction("Drink");
          while (getMyPlayer().getAnimation() != -1) {
            sleep(random(300, 600));
          }
        }
      }
    }

    if (paramString.equals("Normal Defense")) {
      for (k : this.normalDefense) {
        if (this.skills.getCurrentLevel(1) > this.skills.getRealLevel(1) + random(2, 4))
          continue;
        if (this.inventory.getCount(new int[] { k }) > 0) {
          this.inventory.getItem(this.normalDefense).doAction("Drink");
          while (getMyPlayer().getAnimation() != -1) {
            sleep(random(300, 600));
          }
        }
      }
    }

    if (paramString.equals("Combat potion"))
      for (k : this.combatPots) {
        if ((this.skills.getCurrentLevel(2) > this.skills.getRealLevel(2) + random(2, 4)) && (this.skills.getCurrentLevel(0) > this.skills.getRealLevel(0) + random(2, 4)))
        {
          continue;
        }
        if (this.inventory.getCount(new int[] { k }) > 0) {
          this.inventory.getItem(this.combatPots).doAction("Drink");
          while (getMyPlayer().getAnimation() != -1)
            sleep(random(300, 600));
        }
      }
  }

  private String buildString(int paramInt)
  {
    if (paramInt >= 100000)
      return new StringBuilder().append(paramInt / 1000).append("K").toString();
    double d;
    if (paramInt >= 10000) {
      d = round(paramInt / 1000.0D, 1);
      return new StringBuilder().append(d).append("K").toString();
    }
    if (paramInt >= 1000) {
      d = round(paramInt / 1000.0D, 2);
      return new StringBuilder().append(d).append("K").toString();
    }
    return new StringBuilder().append(paramInt).append("").toString();
  }

  private static double round(double paramDouble, int paramInt) {
    BigDecimal localBigDecimal = new BigDecimal(Double.toString(paramDouble));

    localBigDecimal = localBigDecimal.setScale(paramInt, 4);
    return localBigDecimal.doubleValue();
  }

  private boolean shouldDrinkPot() {
    return (this.skills.getCurrentLevel(2) <= this.skills.getRealLevel(2) + random(3, 5)) || (this.skills.getCurrentLevel(0) <= this.skills.getRealLevel(0) + random(3, 5)) || (this.skills.getCurrentLevel(1) <= this.skills.getRealLevel(1) + random(3, 5)) || (this.skills.getCurrentLevel(4) <= this.skills.getRealLevel(4) + random(3, 5));
  }

  private RSNPC getNearestFree(int[] paramArrayOfInt)
  {
    int i = 20;
    Object localObject = null;
    RSNPC[] arrayOfRSNPC = this.npcs.getAll();

    RSNPC localRSNPC = arrayOfRSNPC[random(0, arrayOfRSNPC.length)];
    try {
      for (int m : paramArrayOfInt) {
        if ((m != localRSNPC.getID()) || (localRSNPC.isInCombat())) {
          continue;
        }
        int n = this.calc.distanceTo(localRSNPC);
        if (n < i) {
          i = n;
          localObject = localRSNPC;
        }
      }
    } catch (Exception localException) {
      localException.printStackTrace();
    }
    return localObject;
  }

  private void getRndSpec() {
    this.rndSpec = random(this.specialCost, 1000);
    log(new StringBuilder().append("Will next Spec at ").append(this.rndSpec / 10).append("%").toString());
  }

  private void drawRect(Graphics paramGraphics, Rectangle paramRectangle, Color paramColor) {
    paramGraphics.setColor(paramColor);
    paramGraphics.drawRect(paramRectangle.x, paramRectangle.y, paramRectangle.width, paramRectangle.height);
  }

  private void drawNiceBox(Graphics paramGraphics, String paramString, int paramInt1, int paramInt2, Color paramColor1, Color paramColor2, Color paramColor3, Boolean paramBoolean)
  {
    int i = 0;
    int j = 0;
    Font localFont;
    Object localObject;
    Rectangle2D localRectangle2D;
    if (paramBoolean.booleanValue()) {
      localFont = new Font("Arial", 1, 16);
      localObject = new YakAttack.1(this, localFont);

      localRectangle2D = ((FontMetrics)localObject).getStringBounds(paramString, null);
      i = (int)localRectangle2D.getWidth();
      paramGraphics.setFont(localFont);
    } else {
      localFont = new Font("Arial", 0, 12);
      localObject = new YakAttack.2(this, localFont);

      localRectangle2D = ((FontMetrics)localObject).getStringBounds(paramString, null);
      i = (int)localRectangle2D.getWidth();
      paramGraphics.setFont(localFont);
    }
    j = (int)localRectangle2D.getHeight() + 6;
    paramGraphics.setColor(paramColor1);
    paramGraphics.fill3DRect(paramInt1, paramInt2, i + 10, j, true);
    paramGraphics.setColor(paramColor2);
    paramGraphics.drawRect(paramInt1 - 1, paramInt2 - 1, i + 11, j + 1);
    paramGraphics.setColor(new Color(paramColor3.getRed(), paramColor3.getGreen(), paramColor3.getBlue(), 30));
    paramGraphics.drawString(paramString, paramInt1 + 4, paramInt2 + j - 5);
    paramGraphics.setColor(paramColor3);
    paramGraphics.drawString(paramString, paramInt1 + 5, paramInt2 + j - 6);
  }

  private void drawItem(Graphics paramGraphics, RSItem paramRSItem, Boolean paramBoolean) throws InterruptedException
  {
    if (paramBoolean.booleanValue()) {
      paramGraphics.setColor(Color.WHITE);
      drawRect(paramGraphics, paramRSItem.getComponent().getArea(), Color.WHITE);
      drawNiceBox(paramGraphics, new StringBuilder().append("Eating: ").append(paramRSItem.getName().substring(paramRSItem.getName().indexOf("<col=ff9040>") + 12)).toString(), paramRSItem.getComponent().getArea().x - 80, paramRSItem.getComponent().getArea().y + paramRSItem.getComponent().getArea().height, new Color(255, 255, 255, 100), new Color(0, 0, 0, 150), Color.BLACK, Boolean.valueOf(false));
    }
    else
    {
      paramGraphics.setColor(Color.GREEN);
      drawRect(paramGraphics, paramRSItem.getComponent().getArea(), Color.GREEN);
      drawNiceBox(paramGraphics, new StringBuilder().append("Using: ").append(paramRSItem.getName()).toString(), paramRSItem.getComponent().getArea().x - 80, paramRSItem.getComponent().getArea().y + paramRSItem.getComponent().getArea().height, new Color(0, 255, 0, 100), new Color(0, 0, 0, 150), Color.BLACK, Boolean.valueOf(false));
    }
  }

  private int statInfo(int paramInt)
  {
    switch (paramInt) {
    case 0:
      return 6;
    case 1:
      return 0;
    case 2:
      return 1;
    case 3:
      return 3;
    case 4:
      return 5;
    case 5:
      return 4;
    case 6:
      return 18;
    case 7:
      return 2;
    }
    return paramInt;
  }

  private int getAngleToCoord(RSTile paramRSTile)
  {
    int i = getMyPlayer().getLocation().getX();
    int j = getMyPlayer().getLocation().getY();
    int k = i - paramRSTile.getX();
    int m = j - paramRSTile.getY();
    double d = Math.toDegrees(Math.atan2(m, k));
    return (int)d;
  }

  private Point centerPoint(RSModel paramRSModel, int paramInt) {
    if (paramRSModel == null)
      return new Point(-1, -1);
    int i = 0;
    int j = 0;
    Point[] arrayOfPoint = new Point[paramInt];
    for (int k = 0; k < paramInt; k++) {
      arrayOfPoint[k] = paramRSModel.getPoint();
      i = (int)(i + arrayOfPoint[k].getX());
      j = (int)(j + arrayOfPoint[k].getY());
    }
    return new Point(i / paramInt, j / paramInt);
  }

  private boolean clickNPC(RSNPC paramRSNPC, String paramString) {
    for (int i = 0; i < 10; i++) {
      if (isPartiallyOnScreen(paramRSNPC.getModel()))
      {
        Point localPoint;
        if (paramRSNPC.isOnScreen())
          localPoint = paramRSNPC.getModel().getCentralPoint();
        else {
          localPoint = getPointOnScreen(paramRSNPC.getModel(), false);
        }
        if ((localPoint == null) || (!this.calc.pointOnScreen(localPoint))) {
          continue;
        }
        this.mouse.move(localPoint, 3, 3);
        String[] arrayOfString = this.menu.getItems();
        if ((arrayOfString.length > 0) && (arrayOfString[0].contains(paramString))) {
          this.mouse.click(true);
          return true;
        }if (this.menu.contains(paramString)) {
          this.mouse.click(false);
          sleep(random(100, 200));
          for (int k = 0; (k < 4) && 
            (this.menu.contains(paramString)); k++)
          {
            if (this.menu.doAction(paramString)) {
              return true;
            }
          }
        }
      }
      else if ((paramRSNPC != null) && (!paramRSNPC.isOnScreen())) {
        this.Status = "Setting view to yaks";
        int j = this.camera.getCharacterAngle(paramRSNPC);
        if ((this.calc.distanceTo(paramRSNPC) < 10) && (Math.abs(j - this.camera.getAngle()) > 20))
        {
          this.camera.turnTo(paramRSNPC);
        }
      } else {
        this.walking.walkTileMM(closerTile(paramRSNPC.getLocation(), 1), 2, 2);
        return false;
      }
    }

    return false;
  }

  private boolean isPartiallyOnScreen(RSModel paramRSModel) {
    return getPointOnScreen(paramRSModel, true) != null;
  }

  private Point getPointOnScreen(RSModel paramRSModel, boolean paramBoolean) {
    if (paramRSModel == null)
      return null;
    ArrayList localArrayList = new ArrayList();
    try {
      Polygon[] arrayOfPolygon = paramRSModel.getTriangles();
      for (int i = 0; i < arrayOfPolygon.length; i++) {
        Polygon localPolygon = arrayOfPolygon[i];
        for (int j = 0; j < localPolygon.xpoints.length; j++) {
          Point localPoint = new Point(localPolygon.xpoints[j], localPolygon.ypoints[j]);
          if (this.calc.pointOnScreen(localPoint)) {
            if (paramBoolean)
              return localPoint;
            localArrayList.add(localPoint);
          }
        }
      }
    } catch (Exception localException) {
    }
    return localArrayList.size() > 0 ? (Point)localArrayList.get(random(0, localArrayList.size())) : null;
  }

  private RSTile closerTile(RSTile paramRSTile, int paramInt) {
    RSTile localRSTile = getMyPlayer().getLocation();
    int i = paramRSTile.getX(); int j = paramRSTile.getY();
    for (int k = 1; k < paramInt; k++) {
      i = paramRSTile.getX() != localRSTile.getX() ? i++ : paramRSTile.getX() < localRSTile.getX() ? i-- : i;

      j = paramRSTile.getY() != localRSTile.getY() ? j++ : paramRSTile.getY() < localRSTile.getY() ? j-- : j;
    }

    return new RSTile(i, j);
  }

  private int getAction() {
    if (this.game.isLoggedIn()) {
      if ((getMyPlayer().getInteracting() != null) || (getMyPlayer().isInCombat()))
      {
        if ((this.doSpec) && (this.settings.getSetting(300) >= this.rndSpec)) {
          this.rndSpecCtr = 0;
          return 2;
        }
      }
      if (this.calc.distanceTo(this.yakTile) < 50) {
        return 0;
      }
      return 1;
    }
    return random(100, 300);
  }

  public int loop()
  {
    this.foodInBag = this.inventory.getItem(this.foodID);
    RSNPC localRSNPC = this.npcs.getNearest(new YakAttack.3(this));

    if ((this.wants2Eat) && 
      (this.combat.getLifePoints() <= random(this.hp2EatAt - 30, this.hp2EatAt + 30))) {
      this.Status = "Eating Food";
      if (this.inventory.contains(this.Food)) {
        this.inventory.getItem(new int[] { this.Food }).doAction("Eat");
        return random(800, 1400);
      }if ((!this.inventory.contains(this.Food)) && (this.inventory.contains(this.Food)))
      {
        this.inventory.getItem(this.foodID).doAction("Eat");
        return random(800, 1400);
      }
      this.Status = "Out of food! shutting down";
      log("We are out of food! logging out");
      if (getMyPlayer().isInCombat()) {
        sleep(random(10000, 11000));
      }
      stopScript(true);
    }

    if (this.inventory.getCount(this.Junk) >= 1) {
      this.Status = "Dropping junk";
      this.inventory.getItem(this.Junk).doAction("Drop");
    }

    if (getMyPlayer().getInteracting() != null)
      this.Status = "Engaged in Combat";
    else {
      this.Status = "We are idle";
    }

    while ((this.Wait) && (
      ((getMyPlayer().getInteracting() instanceof RSNPC)) || (getMyPlayer().isInCombat())));
    if ((this.arrowID != -1) && (getMyPlayer().getInteracting() == null) && (this.groundItems.getNearest(this.arrowPack) != null))
    {
      handleArrows();
    }
    else if (localRSNPC != null) {
      Point localPoint = localRSNPC.getScreenLocation();
      if ((localPoint != null) && (getMyPlayer().isInCombat())) {
        this.Status = "Hovering Yaks";
        this.mouse.move(new Point(centerPoint(localRSNPC.getModel(), random(3, 5))));
      }

    }

    if (this.doSpec) {
      if ((this.rndSpecCtr == 0) && (this.settings.getSetting(300) >= this.specialCost)) {
        if (this.settings.getSetting(301) != 1) {
          this.rndSpec = this.settings.getSetting(300);
        } else if (this.settings.getSetting(301) == 1) {
          int i = this.settings.getSetting(300) - this.specialCost;
          if (i >= this.specialCost)
            this.rndSpec = i;
          else {
            getRndSpec();
          }
        }
        this.rndSpecCtr = 1;
      }
      else if (this.settings.getSetting(300) < this.specialCost) {
        if (this.inventory.containsOneOf(new int[] { this.CURR_WEP_ID })) {
          this.inventory.getItem(new int[] { this.CURR_WEP_ID }).doAction("Wield");
          sleep(random(1000, 1100));
          this.mouse.moveSlightly();
        }
        if (this.rndSpecCtr == 0) {
          getRndSpec();
          this.rndSpecCtr = 1;
        }
      }

    }

    switch (getAction()) {
    case 2:
      if (!this.inventory.containsOneOf(new int[] { this.SPEC_WEP_ID })) break;
      this.inventory.getItem(new int[] { this.SPEC_WEP_ID }).doAction("Wield");
      sleep(random(1000, 1100));
    case 0:
    case 1:
      while ((getMyPlayer().getInteracting() != null) && (this.settings.getSetting(300) >= this.specialCost) && 
        (getMyPlayer().getInteracting() != null))
      {
        if (this.game.getCurrentTab() != 0) {
          this.game.openTab(0);
          sleep(random(400, 600));
        }
        if (this.settings.getSetting(301) != 1) {
          this.interfaces.get(884).getComponent(4).doClick();
          sleep(random(900, 1000)); continue;
        }
        sleep(random(100, 300)); continue;

        runControl();

        RSGroundItem localRSGroundItem = this.groundItems.getNearest(new int[] { this.arrowID });
        if ((localRSGroundItem != null) && (this.arrowID != -1)) {
          if (this.inventory.getCount(new int[] { this.arrowID }) == random(50, 100)) {
            if (this.game.getCurrentTab() != 4) {
              this.game.openTab(4);
            }
            this.Status = "Equiping Arrows";
            this.inventory.getItem(new int[] { this.arrowID }).doAction("Wield");
            return random(15000, 30000);
          }
        }
        else localRSGroundItem = null;

        if ((this.inventory.getCount(this.potionSet) >= 1) && (shouldDrinkPot())) {
          for (String str : this.potionsToDrink) {
            drinkPot(str);
          }
        }

        if ((localRSNPC == null) || (getMyPlayer().getInteracting() != null)) break;
        this.Status = "Attacking Yaks";
        if (clickNPC(localRSNPC, "Attack"))
          this.mouse.moveOffScreen();
        return random(400, 600);

        this.log.warning("Stopping script get to the Yak Pen on Neitiznot.");
        stopScript(true);
        return random(100, 200);
      }
    }
    return random(200, 400);
  }

  public boolean onStart()
  {
    this.gui = new YakAttack.YakAttackProGUI(this);
    this.gui.setVisible(true);
    while (this.guiWait) {
      if (!this.gui.isVisible())
        return false;
      sleep(100);
    }
    this.gui.setVisible(false);
    if (this.guiExit) {
      return false;
    }
    for (int i = 0; i < this.startingexp.length; i++) {
      if (this.startinglvl[i] >= 1)
        continue;
      this.startingexp[i] = this.skills.getCurrentExp(statInfo(i));
    }
    for (i = 0; i < this.startinglvl.length; i++) {
      if (this.startinglvl[i] >= 1)
        continue;
      this.startinglvl[i] = this.skills.getRealLevel(statInfo(i));
    }

    log("************************************************");
    log(new StringBuilder().append("********YakAttack V").append(this.properties.version()).append(" started!**************").toString());

    log("*********Let's get some Experience**************");
    log("************************************************");
    this.mouse.setSpeed(this.speed);
    this.camera.setPitch(true);
    if (!this.t.isAlive()) {
      this.t.start();
      log("Built-in camera initialized!");
    }
    return true;
  }

  private void runControl() {
    if ((!this.walking.isRunEnabled()) && (this.walking.getEnergy() > random(20, 30)))
      this.walking.setRun(true);
  }

  public void messageReceived(MessageEvent paramMessageEvent)
  {
    String str = paramMessageEvent.getMessage();

    if (str.contains("System update in")) {
      this.log.warning("There will be a system update soon, so we logged out");
      stopScript(true);
    }
    if (str.contains("Oh dear, you are dead!")) {
      this.Status = "Dead";
      this.log.warning("We somehow died :S, shutting down");
      stopScript(true);
    }
    if (str.contains("already under attack")) {
      sleep(random(2500, 4000));
    }
    if (str.contains("There is no ammo left in your quiver.")) {
      this.log.warning("We have no arrows left, shutting down!");
      stopScript(true);
    }

    if (str.contentEquals("You can't log out until 10 seconds after the end of combat."))
    {
      log("Waiting 10 seconds before logging out");
      sleep(random(10100, 11000));
      stopScript(true);
    }
    if (str.contains("You've just advanced an")) {
      log("Congrats on level up, Screenshot taken!");
      this.env.saveScreenshot(true);
      this.interfaces.clickContinue();
    }
  }

  private String locToString() {
    return new StringBuilder().append("(X: ").append(getMyPlayer().getLocation().getX()).append(", Y:").append(getMyPlayer().getLocation().getY()).append(")").toString();
  }

  public void onRepaint(Graphics paramGraphics)
  {
    ((Graphics2D)paramGraphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    int i = this.mouse.getLocation().x;
    int j = this.mouse.getLocation().y;
    int k = this.mouse.getPressLocation().x;
    int m = this.mouse.getPressLocation().y;
    long l1 = this.mouse.getPressTime();
    if ((this.game.isLoggedIn()) && (!this.guiWait))
      if (System.currentTimeMillis() - l1 < 100L) {
        paramGraphics.setColor(new Color(70, 130, 180, 250));
        paramGraphics.drawString("C", k, m);
      } else if ((System.currentTimeMillis() - l1 < 200L) && (System.currentTimeMillis() - l1 > 99L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 225));
        paramGraphics.drawString("Cl", k, m);
      } else if ((System.currentTimeMillis() - l1 < 300L) && (System.currentTimeMillis() - l1 > 199L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 200));
        paramGraphics.drawString("Cli", k, m);
      } else if ((System.currentTimeMillis() - l1 < 400L) && (System.currentTimeMillis() - l1 > 299L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 175));
        paramGraphics.drawString("Clic", k, m);
      } else if ((System.currentTimeMillis() - l1 < 500L) && (System.currentTimeMillis() - l1 > 399L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 150));
        paramGraphics.drawString("Click", k, m);
      } else if ((System.currentTimeMillis() - l1 < 600L) && (System.currentTimeMillis() - l1 > 499L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 125));
        paramGraphics.drawString("Click", k, m);
      } else if ((System.currentTimeMillis() - l1 < 700L) && (System.currentTimeMillis() - l1 > 599L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 100));
        paramGraphics.drawString("Click", k, m);
      } else if ((System.currentTimeMillis() - l1 < 800L) && (System.currentTimeMillis() - l1 > 699L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 75));
        paramGraphics.drawString("Click", k, m);
      } else if ((System.currentTimeMillis() - l1 < 900L) && (System.currentTimeMillis() - l1 > 799L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 50));
        paramGraphics.drawString("Click", k, m);
      } else if ((System.currentTimeMillis() - l1 < 1000L) && (System.currentTimeMillis() - l1 > 899L))
      {
        paramGraphics.setColor(new Color(70, 130, 180, 25));
        paramGraphics.drawString("Click", k, m);
      }
    Polygon localPolygon = new Polygon();
    localPolygon.addPoint(i, j);
    localPolygon.addPoint(i, j + 15);
    localPolygon.addPoint(i + 10, j + 10);
    paramGraphics.setColor(new Color(70, 130, 180, 125));
    paramGraphics.fillPolygon(localPolygon);
    paramGraphics.drawPolygon(localPolygon);
    try
    {
      Point localPoint = new Point(this.mouse.getLocation().x, this.mouse.getLocation().y);

      if ((this.foodInBag != null) && (this.foodInBag.getName() != null)) {
        if ((this.Status.equals("Eating food")) && (this.inventory.getInterface().getArea().contains(localPoint)))
          try
          {
            drawItem(paramGraphics, this.foodInBag, Boolean.valueOf(true));
          } catch (Exception localException2) {
          }
      }
      else this.foodInBag = null;
    }
    catch (Exception localException1)
    {
    }
    for (int n = 0; n < this.currentlvl.length; n++) {
      this.currentlvl[n] = this.skills.getRealLevel(statInfo(n));
      this.currentexp[n] = this.skills.getCurrentExp(statInfo(n));
      this.diffexp[n] = (this.currentexp[n] - this.startingexp[n]);
    }

    StringBuilder localStringBuilder1 = new StringBuilder();
    long l2 = this.timeRan.getElapsed();
    long l3 = l2 / 1000L;
    long l4 = l3 / 60L;
    long l5 = l4 / 60L;
    long l6 = l5 / 24L;
    int i1 = (int)l3 % 60;
    int i2 = (int)l4 % 60;
    int i3 = (int)l5 % 60;
    int i4 = (int)l6;
    if (i4 > 3) {
      localStringBuilder1.append(" ");
      localStringBuilder1.append(buildString(i4));
      localStringBuilder1.append(" Days");
    } else {
      if (i3 < 10)
        localStringBuilder1.append("0");
      localStringBuilder1.append(i3);
      localStringBuilder1.append(" : ");
      if (i2 < 10)
        localStringBuilder1.append("0");
      localStringBuilder1.append(i2);
      localStringBuilder1.append(" : ");
      if (i1 < 10)
        localStringBuilder1.append("0");
      localStringBuilder1.append(i1);
    }
    int i5 = 0;
    int i6 = 0;
    String str1 = "";
    StringBuilder localStringBuilder2 = new StringBuilder();

    if (l3 > 0L) {
      for (int i7 = 0; i7 < this.diffexp.length; i7++) {
        if (this.diffexp[i7] > 0) {
          int i8 = this.diffexp[i7] * 60 * 60 / (int)l3;
          int i9 = this.exptolevel[this.currentlvl[i7]] - this.currentexp[i7];
          StringBuilder localStringBuilder3 = new StringBuilder();
          long l7 = i9 * this.timeRan.getElapsed() / this.diffexp[i7];

          long l8 = l7 / 1000L;
          long l9 = l8 / 60L;
          long l10 = l9 / 60L;
          long l11 = l10 / 24L;
          i21 = (int)l8 % 60;
          int i22 = (int)l9 % 60;
          i23 = (int)l10 % 60;
          int i24 = (int)l11;
          if (i24 > 3) {
            localStringBuilder3.append(" ");
            localStringBuilder3.append(buildString(i24));
            localStringBuilder3.append(" Days");
          } else {
            if (i23 < 10)
              localStringBuilder3.append("0");
            localStringBuilder3.append(i23);
            localStringBuilder3.append(" : ");
            if (i22 < 10)
              localStringBuilder3.append("0");
            localStringBuilder3.append(i22);
            localStringBuilder3.append(" : ");
            if (i21 < 10)
              localStringBuilder3.append("0");
            localStringBuilder3.append(i21);
          }

          str1 = new StringBuilder().append(str1).append((this.currentexp[i7] - this.exptolevel[(this.currentlvl[i7] - 1)]) * 100 / (this.exptolevel[this.currentlvl[i7]] - this.exptolevel[(this.currentlvl[i7] - 1)])).append("qaswed").toString();

          localStringBuilder2.append(new StringBuilder().append(this.statnames[i7]).append(" ").append(this.currentlvl[i7]).append('/').append(this.startinglvl[i7]).append("  |  EXP-TL: ").append(buildString(i9)).append("  |  Gain: ").append(buildString(this.diffexp[i7])).append("  |  EXP/HR: ").append(buildString(i8)).append("  |  TTL: ").append(localStringBuilder3).append("qaswed").toString());

          i5 += i8;
          i6 += this.diffexp[i7];
        }

      }

    }

    String str2 = new StringBuilder().append("Time ran: ").append(localStringBuilder1).append("  |  Total Gain: ").append(buildString(i6)).append("  |  Total EXP/HR: ").append(buildString(i5)).append("  |  Location: ").append(locToString()).append("qaswed").append(localStringBuilder2.toString()).toString();

    String[] arrayOfString1 = str2.split("qaswed");
    String[] arrayOfString2 = new StringBuilder().append("100qaswed").append(str1).toString().split("qaswed");

    int i10 = 120;
    int i11 = 90;
    int i12 = 30;

    int i13 = 4;
    int i14 = 3;

    int i15 = arrayOfString1.length;
    int i16 = 9;
    int i17 = 346;
    int i18 = 485;
    int i19 = 17;
    int i20 = i19 * i15;
    paramGraphics.setColor(new Color(0, 0, 0, 130));
    paramGraphics.fillRoundRect(i16 - 2, i17 - 2, i18 + 5, 5 + i20, 10, 10);

    for (int i21 = 0; i21 < i15; i21++) {
      paramGraphics.setColor(new Color(i10, i11, i12, 190));
      paramGraphics.drawRoundRect(i16, i17 + 1 + i21 * i19, i18, i19 - 2, 10, 10);

      paramGraphics.fillRoundRect(i16, i17 + 1 + i21 * i19, i18, i19 - 2, 10, 10);

      paramGraphics.setColor(new Color(i10 * 2 / 3, i11 * 2 / 3, i12 * 2 / 3, 190));

      paramGraphics.fillRoundRect(i16, i17 + 1 + i21 * i19, i18 * Integer.parseInt(arrayOfString2[i21]) / 100, i19 - 2, 10, 10);

      paramGraphics.setColor(Color.BLACK);
      paramGraphics.drawString(arrayOfString1[i21], i16 + 5, i17 - i14 + (i21 + 1) * i19);

      paramGraphics.setColor(Color.WHITE);
      paramGraphics.drawString(arrayOfString1[i21], i16 + 5, i17 - i13 + (i21 + 1) * i19);
    }

    i21 = (int)(3600000.0D / this.timeRan.getElapsed() * i6);
    this.yaksKilled = (i6 / 200);
    this.yaksPerHour = (i21 / 200);
    String[] arrayOfString3 = { new StringBuilder().append(this.properties.name()).append(" v").append(this.properties.version()).toString(), new StringBuilder().append("Status: ").append(this.Status).toString(), new StringBuilder().append("Yaks Killed: ").append(this.yaksKilled).append("  |  YaksKilled/HR: ").append(this.yaksPerHour).toString(), "www.powerbot.org" };

    i16 = 5;
    i17 = 30;
    i18 = 210;
    i19 = 16;
    i15 = arrayOfString3.length;
    i20 = i19 * i15;

    paramGraphics.setColor(new Color(0, 0, 0, 130));
    paramGraphics.fillRoundRect(i16 - 2, i17 - 2, i18 + 5, 5 + i20, 10, 10);

    paramGraphics.setColor(new Color(i10, i11, i12, 120));
    paramGraphics.drawRoundRect(i16, i17 + 1 + 0 * i19, i18, i19 - 2, 10, 10);

    paramGraphics.fillRoundRect(i16, i17 + 1 + 0 * i19, i18, i19 - 2, 10, 10);

    for (int i23 = 0; i23 < i15; i23++) {
      paramGraphics.setColor(Color.BLACK);
      paramGraphics.drawString(arrayOfString3[i23], i16 + 5, i17 - i14 + (i23 + 1) * i19);

      paramGraphics.setColor(Color.WHITE);
      paramGraphics.drawString(arrayOfString3[i23], i16 + 5, i17 - i13 + (i23 + 1) * i19);
    }
  }

  public void onFinish()
  {
    this.env.saveScreenshot(true);
    this.log.info(new StringBuilder().append("Ran for ").append(this.timeRan.toElapsedString()).toString());
    if (!this.players.getMyPlayer().isInCombat())
      stopScript(false);
  }
}